package com.wanggsx.maindemo;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Utils {

    public static String copyAssetFileToAppCacheDir(Context context,String fileName) throws IOException {
        File cacheDir = context.getCacheDir();
        if (!cacheDir.exists()){
            cacheDir.mkdirs();
        }
        File outputFile = new File(cacheDir,fileName);
        if (outputFile.exists())outputFile.delete();
        if (!outputFile.exists()){
            boolean tf = outputFile.createNewFile();
            if (tf){
                InputStream in = context.getAssets().open(fileName);
                FileOutputStream out = new FileOutputStream(outputFile);
                byte[] buffer = new byte[in.available()];
                int len;
                while ((len=in.read(buffer))!= -1){
                    out.write(buffer,0,len);
                }
                out.flush();
                in.close();
                out.close();
                return outputFile.getAbsolutePath();
            }
        }
        return null;
    }

}
